<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class Classroom extends Model
{
    public function school()
    {
        return $this->hasOne(School::class,'id','school_id');
    }

    public function getList1($time = null, $schoolId = null, $name = null, $teacherName = null,$id=null)
    {

        $time=array_values($time);

        return $this->alias('cr')
            ->join('classtime ct','ct.classroom_id=cr.id')
            ->join('class_student cs', 'ct.id=cs.classtime_id')
            ->join('student s', 's.id=cs.student_id')
            ->join('invoice_details ivd', 'ivd.id=cs.invoice_details_id')
            ->join('invoice i', 'i.id=ivd.invoice_id')
            ->join('course', 'course.id=ct.course_id')
            ->join('admins a', 'a.id=ct.admin_id')
            ->when($schoolId,function ($q)use($schoolId){
                    $q->where('cr.school_id',$schoolId);
            })
            ->when($name,function ($q)use($name){
                $q->where('s.name','like',"%$name%");
            })
            ->when($teacherName,function ($q)use($teacherName){
                $q->where('a.name','like',"%$teacherName%");
            })
            ->when($id,function ($q)use($id){
                $q->where('cs.student_id',$id);
            })
            ->where('cs.state','<>','取消')
            ->whereBetween('ct.start_time', $time)
            ->whereBetween('ct.end_time', $time)
            ->field('cr.id,cr.school_id,cr.name,cr.number,cr.color,cr.sort')
            ->group('cr.id')
            ->order('cr.sort','desc')
            ->order('cr.id')
            ->select()
            ->toArray();

    }
}
